#include "StdAfx.h"
#include "dll_object.h"
#include "msgmanager.h"
#include <interface/interface.h>
#include <interface/xml_parser_interface.h>
#include <interface/log_interface.h>
#include <utility/api_tools.h>
#include <Util/WinAPITools.h>
#include "dlg_wrapper.h"
#include <ui/dialogbase.h>

dll_object* g_dll_obj = NULL;
aot::tt::imain*	dll_object::im_ = NULL;
dlg_wrapper*	dll_object::impl_ = NULL;
dll_object::dll_object()
{
	this->impl_ = NULL;
	this->im_ = NULL;
	this->show_tab_ = aot::e_tab_sms;
}

dll_object::~dll_object()
{
	AFX_MANAGE_STATE(AfxGetStaticModuleState());
 	if(this->impl_)
 	{
		this->impl_->DestroyWindow();
 		this->impl_ = NULL;
 	}
}

void 
dll_object::destroy()
{
	AFX_MANAGE_STATE(AfxGetStaticModuleState());
	if(0 == dec_ref())
		delete this;
}


bool 
dll_object::on_regist(aot::tt::idll::dll_regist_param_t* p, aot::tt::result_t* ret)
{	
	AFX_MANAGE_STATE(AfxGetStaticModuleState());
	this->im_ = p->mi;
	g_dll_obj->im_ = this->im_;
	this->main_wnd_ = p->parent_wnd;
	init_log(this->im_);
	regist_to_trans_msg(p, ret);

	
	if ( !this->global_.init(this->im_) )
	{
		return false;
	}
	
	if ( !this->interface_.init_component_query(this->im_, this) )
	{
		return false;
	}

	if ( !g_multi_lang_impl.init() )
	{
		return false;
	}

	regist_sys_event(p);
	return true;
}

int	
dll_object::on_putq (aot::tt::message_t* msg, long tm, aot::tt::result_t* ret)
{
	AFX_MANAGE_STATE(AfxGetStaticModuleState());

	return -1;
}

int	
dll_object::on_command (aot::tt::command_t* cmd, aot::tt::result_t* ret)
{
	AFX_MANAGE_STATE(AfxGetStaticModuleState());

	if ( cmd->type == WM_GLB_UI_SHOW_MSGMANAGER )
	{
		show_tab_ = (aot::emsgmanger_tab_type)cmd->val;
		if ( !on_show_window(NULL, NULL, true, NULL) )
		{
			return -1;
		}
		

		if ( show_tab_ == aot::e_tab_chat_record )
		{
			int chat_type = cmd->user_data[0];
			if ( chat_type != -1)
			{
				std::string session_id = (const char*)cmd->user_data_ptr[0];
				this->impl_->show_chat_log( chat_type, session_id);
			}
		}
		else if ( this->show_tab_ == aot::e_tab_phone_record )
		{
			int type = cmd->user_data[0];
			if ( type == 1)
			{
				std::string user_id = (const char*)cmd->user_data_ptr[0];
				this->impl_->show_phone_record( user_id);
			}
			else if ( type == 2)
			{
				this->impl_->show_unanswered_calls();
			}
		}
		else if ( this->show_tab_ == aot::e_tab_sms )
		{
			int type = cmd->user_data[0];
			if ( type == 1)
			{
				this->impl_->show_unread_sms();
			}
		}
		else if ( this->show_tab_ == aot::e_tab_calendar )
		{
			int type = cmd->user_data[0];
			if ( type ==1 )
			{
				const char *time_ = (char*)cmd->user_data_ptr[0];
				this->impl_->show_day_calendar(time_);
			}
		}
		return 1;
	}
	else if (cmd->type == WM_LBUTTONDOWN)
	{
		if( this->impl_ )
		{
			if (cmd->val == 1)
			{
				this->impl_->get_chat_rec_tool_bar()->GetDlgItem(IDC_BTN_CHAT_REC_DELETE)->EnableWindow(TRUE);
				this->impl_->get_chat_rec_tool_bar()->GetDlgItem(IDC_BTN_CHAT_REC_CHAT)->EnableWindow(TRUE);
				this->impl_->get_chat_rec_tool_bar()->GetDlgItem(IDC_BTN_CHAT_REC_CALL)->EnableWindow(TRUE);
			} 
			else
			{
				this->impl_->get_chat_rec_tool_bar()->GetDlgItem(IDC_BTN_CHAT_REC_DELETE)->EnableWindow(FALSE);
				this->impl_->get_chat_rec_tool_bar()->GetDlgItem(IDC_BTN_CHAT_REC_CHAT)->EnableWindow(FALSE);
				this->impl_->get_chat_rec_tool_bar()->GetDlgItem(IDC_BTN_CHAT_REC_CALL)->EnableWindow(FALSE);
			}
		}
	}

	if ( this->impl_ )
	{
		this->impl_->on_from_other_dll_notify(cmd, ret);
	}
	
	return -1;
}

bool	
dll_object::on_show_window(HWND parent_wnd, const RECT* r, bool show, aot::tt::result_t* ret/* = NULL*/)
{
	AFX_MANAGE_STATE(AfxGetStaticModuleState());
 	if( show )
 	{
 		if( NULL == this->impl_ )
 		{
 			this->impl_ = new dlg_wrapper();
			if ( !this->impl_->Create(dlg_wrapper::IDD, CWnd::GetDesktopWindow()) )
			{
				delete this->impl_;
				this->impl_ = NULL;
				return false;
			}
			DWORD tm1 = GetTickCount();

			this->impl_->init_data();
			DWORD tm2 = GetTickCount();
			TRACE("msgmanager init_data time %d", tm2 - tm1);
 		}


 		if (!this->impl_->IsWindowVisible())
 		{
 			int xscreen = GetSystemMetrics(SM_CXSCREEN);
 			int yscreen = GetSystemMetrics(SM_CYSCREEN);
 			int height = 536;
 			int width = 802;

 			CRect rect;
 			rect.left = (xscreen - width) / 2;
 			rect.top = (yscreen - height) / 2;
 			rect.bottom = rect.top + height;
 			rect.right = rect.left + width;
 
 			this->impl_->MoveWindow(rect, TRUE);
			this->impl_->set_show_tab(this->show_tab_);
			this->impl_->ShowWindow(SW_SHOWNORMAL);
			::SetForegroundWindow(impl_->GetSafeHwnd());
 		}
		else
		{
			this->impl_->set_show_tab(this->show_tab_);
			this->impl_->ShowWindow(SW_SHOWNORMAL);
			::SetForegroundWindow(impl_->GetSafeHwnd());
		}

		push_show_wnd(dlg_wrapper::IDD, this->impl_);
 	}
 	else
 	{
 		if( this->impl_ )
		{
 			this->impl_->ShowWindow(SW_HIDE);
			del_show_wnd(dlg_wrapper::IDD);
		}
 	}

	return true;
}

void	
dll_object::on_window_size_changed(HWND parent_wnd, const RECT* r)
{
	AFX_MANAGE_STATE(AfxGetStaticModuleState());
 	if( this->impl_  && ::IsWindow(this->impl_->m_hWnd))
 	{
 		this->impl_->MoveWindow(r);
 	}
}

BOOL dll_object::pre_tans_msg( MSG* pMsg )
{
	AFX_MANAGE_STATE(AfxGetStaticModuleState());
	return theApp.PreTranslateMessage(pMsg);
}

bool dll_object::regist_to_trans_msg( aot::tt::idll::dll_regist_param_t* p, aot::tt::result_t* ret )
{
	aot::tt::imain::regist_trans_msg_t rg_param;
	rg_param.is_trans_msg = true;
	rg_param.token = p->token;
	p->mi->regist_to(&rg_param, aot::tt::imain::e_regist_trans_msg, NULL);
	return true;
}

int 
dll_object::on_sys_event( int event_type, int ev, WPARAM wparam, LPARAM lparam, aot::tt::sys_event_param_t* sep /*= NULL*/ )
{
	switch( ev )
	{
	case WM_GLB_UI_LOCK_MAIN: 
		{
			show_window_map_type::iterator it = this->show_windows_.begin();
			while ( it != this->show_windows_.end())
			{
				it->second->ShowWindow(SW_HIDE);
				it++;
			}
		}
		break;
	case WM_GLB_UI_UNLOCK_MAIN:
		{
			show_window_map_type::iterator it = this->show_windows_.begin();
			while ( it != this->show_windows_.end())
			{
				it->second->ShowWindow(SW_SHOW);
				it++;
			}
		}
		break;
	}

	if( sep )
	{
		if( sep->wparam ) sep->wparam->destroy();
		if( sep->lparam ) sep->lparam->destroy();	
	}
	return 0;
}

void dll_object::init_log( aot::tt::imain* imain_obj )
{
	aot::tt::ilog *log_obj = NULL;
	aot::tt::idll* dll = NULL;

	if( !this->im_->query_interface((void**)&log_obj, "ilog") )
	{
		return;
	}


	aot_log_init(log_obj);
	aot_log_cfg_from_file("idll.msgmanager.dll", NULL);

	aot_log_debug(AOT_LM_DEBUG, "%s", "msgmanager log initial ok !");
}

void 
dll_object::regist_sys_event( aot::tt::idll::dll_regist_param_t* p )
{
	int ev1[] = 
	{
		WM_GLB_UI_LOCK_MAIN, 
		WM_GLB_UI_UNLOCK_MAIN, 
	};
	aot::tt::imain::regist_sys_event_t t1;
	t1.event_type = aot::tt::imain::regist_sys_event_t::e_type_ui;
	t1.event_lst = ev1;
	t1.event_num = sizeof(ev1) / sizeof(ev1[0]);
	t1.token = p->token;
	this->im_->regist_to((void*)&t1, aot::tt::imain::e_regist_sys_event);
}

void 
dll_object::push_show_wnd( int idd, CDialogBase* dlg )
{
	show_window_map_type::iterator it = this->show_windows_.find(idd);
	if ( it == this->show_windows_.end() )
	{
		this->show_windows_.insert(show_window_map_type::value_type(idd, dlg));
	}
}

void 
dll_object::del_show_wnd( int idd )
{
	show_window_map_type::iterator it = this->show_windows_.find(idd);
	if ( it != this->show_windows_.end() )
	{
		this->show_windows_.erase(it);
	}
}

global_data_wrapper* get_global()
{
	return &g_dll_obj->global_;
}

interface_wrapper* get_interface()
{
	return &g_dll_obj->interface_;
}
